package codey;

import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.event.WaveletSelfAddedEvent;

@SuppressWarnings("serial")
public class CodeyServlet extends AbstractRobot {

	private final String[] KEYWORDS = { "abstact", "continue", "for", "new",
			"switch", "assert", "default", "goto", "package", "synchronized",
			"boolean", "do", "if", "private", "this", "break", "double",
			"implements", "protected", "throw", "byte", "else", "import",
			"public", "throws", "case", "enum", "instanceOf", "return",
			"transient", "catch", "extends", "int", "short", "try", "char",
			"final", "interface", "static", "void", "class", "finally", "long",
			"strictfp", "volatile", "const", "float", "native", "super",
			"while" };

	@Override
	protected String getRobotName() {
		return "Codey";
	}

	@Override
	protected String getRobotAvatarUrl() {
		return "http://dl.dropbox.com/u/120510/epicbox.jpg";
	}

	@Override
	protected String getRobotProfilePageUrl() {
		return "http://code.google.com/apis/wave/extensions/robots/java-tutorial.html";
	}

	@Override
	public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
		Blip blip = event.getWavelet().reply("\nHi everybody!");
	}
	
/*	public void processEvents(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();

		if (bundle.wasSelfAdded()) {
			addCodeyGadget(wavelet);
		} else {
			if (bundle.blipHasChanged(bundle.getWavelet().getRootBlip())) {
				colourCode(wavelet);
			} else {
				List<Event> submitted = bundle.getBlipSubmittedEvents();
				for (Event event : submitted) {
					if (event.getBlip().getBlipId() == wavelet.getRootBlipId()) {
						indentCode(wavelet);
					}
				}
			}

		}

	}

	private void addCodeyGadget(Wavelet wavelet) {
		// TODO fill
	}

	private void colourCode(Wavelet wavelet) {
		TextView code = wavelet.getRootBlip().getDocument();
		String text = code.getText();
		StringTokenizer tokenizer = new StringTokenizer(text, " \n\t", true);
		int rangeStart = 0;
		while (tokenizer.hasMoreTokens()) {
			String currentToken = tokenizer.nextToken();
			if (!currentToken.equals(" ") && !currentToken.equals("\n")
					&& !currentToken.equals("\t")) {
				for (String keyword : KEYWORDS) {
					if (currentToken.equals(keyword)) {
						code.setAnnotation(new Range(rangeStart, rangeStart
								+ currentToken.length()), "style/fontWeight",
								"bold");
						rangeStart = rangeStart + currentToken.length();
						break;
					}
				}
			}
			rangeStart = rangeStart + currentToken.length();
		}
	}

	*//**
	 * On blip_submit event, corrects the indent of the code, according to basic
	 * rules.
	 * 
	 * @param wavelet
	 *//*
	private void indentCode(Wavelet wavelet) {
		TextView code = wavelet.getRootBlip().getDocument();
		String text = code.getText();
		StringTokenizer tokenizer = new StringTokenizer(text);
		int rangeStart = 0; // index of the beginning of the line we parse
		int indent = 0; // desired indent
		while (tokenizer.hasMoreTokens()) {
			String currentToken = tokenizer.nextToken("\n");
			indent = indent + updateIndent(currentToken);
			int existingIndent = 0; // indent that is already in the line
			while (currentToken.startsWith("\t")) {
				currentToken = currentToken.substring(1);
				existingIndent = existingIndent + 1;
			}
			if (existingIndent <= indent) {
				for (int i = existingIndent; i <= indent; i++) {
					code.insert(rangeStart, "\t");
				}
			} else {
				code.delete(new Range(rangeStart, rangeStart
						+ (existingIndent - indent)));
			}

			// TODO long lines wrapping (~80 characters limit)
		}
	}*/

	/**
	 * Given a line to parse, return the difference in indent between the given
	 * line, and the following line.
	 * 
	 * @param line
	 * @return
	 */
	private int updateIndent(String line) {
		String tempLine = line;
		int indent = 0;
		while (tempLine.contains("{")) {
			indent = indent + 1;
			tempLine = tempLine.substring(tempLine.indexOf("{") + 1);
		}
		while (line.contains("}")) {
			indent = indent - 1;
			line = line.substring(line.indexOf("}") + 1);
		}
		return indent;
	}

}
